use std::any::Any;
use std::fs::{create_dir, create_dir_all, remove_dir, remove_dir_all};
use std::io;
use std::path::Path;

const BASE_DIR: &str = "/Users/lhj/rust-test/";

fn main() {
    let file = Path::new("/Users/lhj/documents/abc.pdf");
    // println!("{}",file.components().collect());
    file.components().into_iter().for_each(|p| {
        println!("{:?}", p);
        println!("{:?}", p.type_id());
    });
}

#[test]
fn create_dir_test() {
    // let path = Path::new("/Users/lhj/rust-test");
    // create_dir(Path::new("/Users/lhj/rust-test")).expect("TODO: panic message");
    create_dir_all(Path::new(format!("{}{}", BASE_DIR, "/dir01").as_str())).expect("TODO: 创建目录失败");
    create_dir(Path::new(format!("{}{}", BASE_DIR, "/dir02").as_str())).expect("TODO: 创建目录失败");
}

// 如果目录不为空，删除失败，提示 Directory not empty
#[test]
fn remove_dir_test() {
    // let path = Path::new("/Users/lhj/rust-test");
    remove_dir(Path::new(BASE_DIR)).expect("TODO: 删除目录失败");
}

#[test]
fn remove_dir_all_test() {
    remove_dir_all(Path::new("/Users/lhj/rust-test")).expect("TODO: 级联删除目录失败");
}

#[test]
fn read_dir_test01()-> io::Result<()>{
    let path = Path::new(BASE_DIR);
    for entry_result in path.read_dir()? {
        let entry = entry_result?;
        println!("{:?}",entry.file_name().to_string_lossy());
        // println!("{:?}",entry.file_type().to_string_lossy());
        // println!("{:?}",entry.path().to_string_lossy());
        // println!("{:?}",entry.metadata().to_string_lossy());
    }
    Ok(())
}


#[test]
fn read_dir_test02(){
    let path = Path::new(BASE_DIR);
    for entry_result in path.read_dir().unwrap() {
        let entry = entry_result.unwrap();
        println!("{:?}",entry.file_name());
        println!("{:?}",entry.file_type());
        println!("{:?}",entry.path());
        println!("{:?}",entry.metadata());
    }
}
